﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity;
using Models;
using IService;
using IDAL;

namespace Service
{
    public class TestServer:ITestServer
    {
        public virtual ITestDAL dal {get;set;}
        public TestServer()
        {
        }
        //public void DelTestData(int id)
        //{
        //    dal.DelTest(id);
        //}

        //public IList<Test> GetTestList()
        //{
        //    DAL.TestDAL dal = new DAL.TestDAL();
        //   return dal.GetTestList();
        //}

        public IList<UploadTaskDTO> GetUploasTaskList(int count)
        {
            IList<UploadTaskDTO> dto = new List<UploadTaskDTO>(); 
            IList<UploadTask> tasks = dal.GetUploasTaskList(count);
            foreach (UploadTask t in tasks)
            {
                UploadTaskDTO task = new UploadTaskDTO();
                task.ExamRequestID = t.ExamRequestID;
                dto.Add(task);
            }
            return dto;
        }

        public OrderViewDTO GetExamRequesByExamID(int examRequestID)
        {
            OrderViewDTO dto = new OrderViewDTO();
            //dto.ServerNode = "";
            //dto.HospitalCode = ""; 
            V_ExamRequestQuery exinfo = dal.GetExamRequesByExamID(examRequestID);
            PatientForUploadDTO patientInfo = new PatientForUploadDTO();
            patientInfo.GlobalPatientId = exinfo.PatientID;
            patientInfo.Name = exinfo.PatientName;
            patientInfo.DateOfBirth =  exinfo.PatientBirthDateTime == null? "":DateTime.Parse(exinfo.PatientBirthDateTime.ToString()).ToShortDateString();
            patientInfo.Gender = exinfo.PatientGenderName;
            dto.PatientInfo = patientInfo;

            OrderForUploadDTO orderinfo = new OrderForUploadDTO();
            orderinfo.AccessionNumber = exinfo.AccessionNo;
            orderinfo.ApplyDepartmentCode = exinfo.DepartmentID.ToString();
            orderinfo.ApplyDepartmentName = exinfo.DepartmentName;
            orderinfo.ApplyDoctorCode = exinfo.ApplyDoctorID.ToString();
            orderinfo.ApplyDoctorName = exinfo.ApplyDoctorName;
            orderinfo.DeviceCode = exinfo.DeviceID.ToString();
            orderinfo.DeviceName = exinfo.DeviceName;
            orderinfo.ModalityCode = exinfo.ModalityID.ToString();
            orderinfo.ModalityName = exinfo.ModalityName;
            orderinfo.CheckItems = string.Format("{0},{1},{2},{3},0", exinfo.BodyPartID.ToString(), exinfo.BodyPartName, exinfo.BodyPartID.ToString(), exinfo.BodyPartName);
            orderinfo.StudyDate = exinfo.ExamTime == null ? "" : DateTime.Parse(exinfo.ExamTime.ToString()).ToString("yyyy-MM-dd HH:mm:ss");
            orderinfo.StudyId = exinfo.StudyInstanceUID;
            dto.OrderInfo = orderinfo;

            VisitForUploadDTO vdto = new VisitForUploadDTO();
            vdto.BedNumber = exinfo.BedNo;
            vdto.RoomNumber = exinfo.RoomNo;
            //vdto.PatientType = exinfo.PatientSourceName;
            dto.VisitInfo = vdto;

            ReportForUploadDTO report = new ReportForUploadDTO();
            report.SubmitDateTime = exinfo.ReportTime == null ? "" : DateTime.Parse(exinfo.ReportTime.ToString()).ToString("yyyy-MM-dd HH:mm:ss");
            report.SubmitDoctorCode = exinfo.ReportDoctorID.ToString();
            report.SubmitDoctorName = exinfo.ReportDoctorName;
            report.ApproveDatetime = exinfo.ReportTime == null ? "" : DateTime.Parse(exinfo.ReportTime.ToString()).ToString("yyyy-MM-dd HH:mm:ss");
            report.ApproveDoctorCode = exinfo.ReviewDoctorID.ToString();
            report.ApproveDoctorName = exinfo.ReviewDoctorName;
            report.Findings = exinfo.EyeSee;
            report.Impression = exinfo.Result;
            dto.ReportInfo = report;
            return dto;
        }
    }
}
